#!/usr/bin/expect
set timeout 10

set port [lindex $argv 0]
set sshIp [lindex $argv 1]
set sshUser [lindex $argv 2]
set sshPass [lindex $argv 3]
set filePath [lindex $argv 4]
set toFilePath [lindex $argv 5]

spawn scp -P ${port} ${filePath} ${sshUser}@${sshIp}:${toFilePath}
expect {
    "*yes/no*" {
        send "yes\n"
        expect "*assword:*"
        send "${sshPass}\r"
        expect {
            "*No such file*" {
                exit 1
            }
            "*assword:*" {
                exit 1
            }
            "*try again*" {
                exit 1
            }
        }
    }
    "*assword*" {
        send "${sshPass}\r"
        expect {
            "*No such file*" {
                exit 1
            }
            "*assword*" {
                exit 1
            }
            "*try again*" {
                exit 1
            }
        }
    }
    "*not known*" {
        exit 1
        expect eof
    }
    "*refused*" {
        exit 1
        expect eof
    }
}
exit